﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using CC.Utilities.Drawing;
using CC.Yacht.Utilities;

namespace CC.Yacht.Windows.Forms
{
    public partial class FireworksPanel : UserControl
    {
        #region Constructor
        public FireworksPanel()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);

            MaxFireworks = 10;
            MinFireworks = 5;

            _FontBrush = new SolidBrush(ForeColor);
            _StringFormat = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center,
                Trimming = StringTrimming.EllipsisWord
            };

            if (!DesignMode)
            {
                _Timer.Enabled = true;
            }
        }
        #endregion

        #region Private Fields
        private readonly DoubleBufferedGraphics _DoubleBufferedGraphics = new DoubleBufferedGraphics();
        private readonly List<Firework> _Fireworks = new List<Firework>();
        private SolidBrush _FontBrush;
        private bool _IsStarted;
        private readonly StringFormat _StringFormat;
        #endregion

        #region Public Properties
        public override sealed Color ForeColor
        {
            get { return base.ForeColor; }
            set
            {
                base.ForeColor = value;

                if (_FontBrush == null || _FontBrush.Color != value)
                {
                    if (_FontBrush != null)
                    {
                        _FontBrush.Dispose();
                    }

                    _FontBrush = new SolidBrush(value);
                }
            }
        }

        public int MaxFireworks { get; set; }

        public int MinFireworks { get; set; }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }
        #endregion

        #region Private Event Handlers
        private void _Timer_Tick(object sender, EventArgs e)
        {
            using (Graphics graphics = CreateGraphics())
            {
                Render(graphics);
            }

            //if (!IsStarted && _Fireworks.Count == 0)
            //{
            //    _Timer.Enabled = false;
            //}
        }
        #endregion

        #region Private Methods
        private void Render(Graphics graphics)
        {
            if (!_DoubleBufferedGraphics.IsInitialized || _DoubleBufferedGraphics.Size != Size)
            {
                _DoubleBufferedGraphics.Initialize(Size);
                _DoubleBufferedGraphics.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                _DoubleBufferedGraphics.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            }

            base.OnPaintBackground(new PaintEventArgs(_DoubleBufferedGraphics.Graphics, ClientRectangle));

            if (!string.IsNullOrEmpty(Text))
            {
                _DoubleBufferedGraphics.Graphics.DrawString(Text, Font, _FontBrush, ClientRectangle, _StringFormat);
            }

            for (int i = _Fireworks.Count - 1; i >= 0; i--)
            {
                Firework firework = _Fireworks[i];
                firework.Run();

                if (firework.IsVisible)
                {
                    firework.Render(_DoubleBufferedGraphics.Graphics);
                }
                else
                {
                    _Fireworks.RemoveAt(i);
                }
            }

            if (_IsStarted && _Fireworks.Count < MaxFireworks)
            {
                int heightStep = Height / 20;
                int minToCreate = (_Fireworks.Count < MinFireworks) ? MinFireworks - _Fireworks.Count : 1;
                int fireworksToCreate = RandomHelper.Next(minToCreate, MaxFireworks - _Fireworks.Count);
                for (int i = 0; i < fireworksToCreate; i++)
                {
                    Firework firework = new Firework(this)
                    {
                        ExplodeHeight = RandomHelper.Next(heightStep, Height - (heightStep * 6)),
                        Location = new Point(RandomHelper.Next(0, Width), Height - 10)
                    };
                    _Fireworks.Add(firework);
                    firework.Render(_DoubleBufferedGraphics.Graphics);
                }
            }

            _DoubleBufferedGraphics.Render(graphics);
        }
        #endregion

        #region Protected Methods
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!_Timer.Enabled)
            {
                Render(e.Graphics);
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (!_Timer.Enabled)
            {
                Render(e.Graphics);
            }
        }
        #endregion

        #region Public Methods
        public void Start()
        {
            _IsStarted = true;

            _Timer.Enabled = true;
        }

        public void Stop()
        {
            _IsStarted = false;
        }
        #endregion
    }
}
